package com.net.test.controller;


import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.net.test.service.ExamService;
import com.net.test.vo.ExamVO;

@Controller
public class ExamController {
	private static final Logger logger = LoggerFactory.getLogger(ExamService.class);
	
	@Autowired
	ExamService examService;
	
	@RequestMapping(value = "/examList.ncb", method = {RequestMethod.GET,RequestMethod.POST})
    public String ExamList(Locale locale, Model model, HttpServletRequest request) throws Exception {
        logger.info("Welcome home! The client locale is {}.", locale);  
        
        List<ExamVO> list = (List<ExamVO>)examService.selectData("testMapper.selectExam");
        
        model.addAttribute("list",list);
        
        return "examList";
	}
	@RequestMapping(value = "/examWrite.ncb", method = {RequestMethod.GET,RequestMethod.POST})
	public String ExamList(Locale locale, Model model)throws Exception{
		
		return "examWrite";
		
		
	}
	@RequestMapping(value = "/examInsert.ncb", method = { RequestMethod.GET, RequestMethod.POST})
		public String ExamInsert(Locale locale, Model model, HttpServletRequest request)throws Exception{
			logger.info("Welcome home! The client locale is {}.", locale);
			
			ExamVO examVO = new ExamVO();
			
			String title = request.getParameter("title");
			String contents = request.getParameter("contents");
			String boardname = "게시판";
			
			examVO.setBoard_Title(title);
			examVO.setBoard_Contents(contents);
			examVO.setBoard_Name(boardname);
			
			int result = examService.insertData("testMapper.insertBoard", examVO);
			
			List<ExamVO> list = (List<ExamVO>)examService.selectData("testMapper.selectExam");
			
			model.addAttribute("list",list);
			
			return "examList";
			
		}
	@RequestMapping(value = "/examDelete.ncb", method = { RequestMethod.GET, RequestMethod.POST})
		public String ExamDelete(Locale locale, Model model, HttpServletRequest request)throws Exception{
			logger.info("Welcome home! The client locale is {}.", locale);
			
			
			
			String delTitle = request.getParameter("del");
			
			int result = examService.deleteData("testMapper.examdeleteBoard", delTitle);
			
			List<ExamVO> list = (List<ExamVO>)examService.selectData("testMapper.selectExam");
			
			model.addAttribute("list",list);
			
			return "examList";
			
	}
	@RequestMapping(value = "examView.ncb", method = {RequestMethod.GET,RequestMethod.POST})
	public String examView(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		String title = request.getParameter("title");	
		
		ExamVO view = (ExamVO)examService.viewData("testMapper.selectExamView",title);
		
		model.addAttribute("view", view);
		
		return "examView";
		
		
	}
	@RequestMapping(value = "examEdit.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String examEdit(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		ExamVO edit = new ExamVO();
		
		String edit1 = request.getParameter("edit1");
		String edit2 = request.getParameter("edit2");
		
		edit.setBoard_Title(edit1);
		edit.setBoard_Contents(edit2);
		
		model.addAttribute("edit", edit);
		
		return "examEdit";
		
		
	}
	@RequestMapping(value = "examUpdate.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String examUpdate(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		
		
		ExamVO update = new ExamVO();
		String uptitle = request.getParameter("uptitle");		
		String title = request.getParameter("title");
		String contents = request.getParameter("contents");
		
		update.setBoard_Title(title);
		update.setBoard_Contents(contents);
		update.setBoard_Name(uptitle);
		
		int result = examService.updateData("testMapper.examUpBoard", update);
		
		List<ExamVO> list = (List<ExamVO>)examService.selectData("testMapper.selectExam");
		
		model.addAttribute("list", list);
		
		return "examList";
		
	}
	
}
